package com.cloud.order.Service.ServiceImpl;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.cloud.order.Feign.ProductFeignClient;
import com.cloud.order.Service.OrderService;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import model.bean.Order;
import model.bean.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

/**
 * @author 韩先楚
 */
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {

    @Autowired
    private DiscoveryClient discoveryClient;
    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @Autowired
    private RestTemplate restTemplate;


 @Autowired
 @Qualifier("com.cloud.order.Feign.ProductFeignClient")
 private ProductFeignClient productFeignClient;

    @Override
    @SentinelResource(value = "createOrder01",blockHandler = "createOrderfallback")
    public Order createOrder(Long productId, Long userId) {
        //使用openfeign远程调用
        Product product = productFeignClient.getProduct01(productId);
//        Product product = getProductRemoveByInterface(productId);
        Order order = new Order();
        order.setId(1L);
        //远程调用 TODO

        order.setAllPrice(product.getPrice());
        order.setAddress("北京市海淀区");
        order.setNickName("韩先楚");
        //远程调用 TODO
        order.setProductList(Arrays.asList(product));
        order.setUserId(userId);
        return order;
    }

    @Override
    public void lll() {
        System.out.println("lll");
    }


    /**
     *   @SentinelResource sentinel对于注解的兜底方法,当标注的的这个方法被限流降级时，会调用这个方法，可以自定义返回值或者抛出异常
     *   底层是使用切面,当检测到注解上有blockHander或fallback时会调用其方法,否则会返回异常
     */
    public Order createOrderfallback(Long productId, Long userId, BlockException ex) {
        Order order = new Order();
        order.setId(1L);
        order.setAllPrice(new BigDecimal(100));
        order.setAddress("未知区域");
        order.setNickName("未知用户");
        return order;
    }

    //负载均衡loadBalance
    public Product getProductRemove(Long productId) {
       //通过locadBalanceClient负载均衡获取服务名实例
        ServiceInstance serviceInstance = loadBalancerClient.choose("service-product");
        String url = "http://"+serviceInstance.getHost()+":"+serviceInstance.getPort()+"/product/"+productId;
        ///远程调用
        log.info("url:{}",url);
        Product forObject = new RestTemplate().getForObject(url, Product.class);
        return forObject;

    }

    public Product getProductRemoveByInterface(Long productId) {
       //基于注解的远程调用,restTemplate加了@LoadBalanced注解，会自动通过loadBalanceClient进行负载均衡
        String url = "http://service-product/product/"+productId; //会自动解析service-product的服务名,并通过loadBalanceClient进行负载均衡
        log.info("url:{}",url);
        Product forObject = restTemplate.getForObject(url, Product.class);
        return forObject;

    }
}
